package com.hmkj.quartz.job;

import com.hmkj.common.utils.ThreadPoolExecutors;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelIndexRealPrice;
import com.hmkj.core.service.cache.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author
 * @date 2017/8/17
 */
@Slf4j
@Component
@Lazy(value = false)
public class QuoteCacheJob {
    @Resource
    RedisService redisService;

    private static final Integer COUNT = 20;


    @Async
    @Scheduled(cron = "0/2 * * * * ?")
    public void stockCache() throws Exception {
        log.info("------------------------股票緩存调用------------------------");
        Set<String> stockSet = redisService.getKeys(CacheID.STOCK_KEYS + "*");
        if (stockSet != null && stockSet.size() > 0) {
            String[] stringList = stockSet.toArray(new String[]{});
            List<String> stockList = new ArrayList<>();
            for (int i = 0; i < stringList.length; i++) {
                stockList.add(stringList[i]);
                if ((i + 1) % COUNT == 0) {
                    execute(stockList);
                    stockList = new ArrayList<>();
                }
            }
            execute(stockList);
        }

        //-----------------行情综合排序-----------------
        Set<String> stockSortSet = redisService.getKeys(CacheID.STOCK_SORT_KEYS + "*");
        if (stockSortSet != null && stockSortSet.size() > 0) {
            stockSortSet.forEach(s -> {
                String value = redisService.get(s);
                ThreadPoolExecutors.getExecutors().execute(() -> {
                    String[] strings = value.split(",");
                    QuotationEndpoint.sortToCache(strings[0], strings[1]);
                });
            });
        }

        //-----------------指数缓存-----------------
        List<ModelIndexRealPrice> list = new ArrayList<>();
//        ModelIndexRealPrice shangzheng = QuotationEndpoint.index("1A0001");
//        ModelIndexRealPrice shenzheng = QuotationEndpoint.index("2A01");
//        ModelIndexRealPrice chuanye = QuotationEndpoint.index("399006");
//        if (shangzheng != null) {
//            list.add(shangzheng);
//        }
//        if (shenzheng != null) {
//            list.add(shenzheng);
//        }
//        if (chuanye != null) {
//            list.add(chuanye);
//        }
        redisService.put(CacheID.STOCK_INDEX,list);
    }

    private void execute(List<String> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            ThreadPoolExecutors.getExecutors().execute(() -> {
                String stocks = list.stream().map(s -> s.substring(CacheID.STOCK_KEYS.length(), s.length())).collect(Collectors.joining(","));
                try {
                    QuotationEndpoint.combStock(stocks);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
